package com.ming.brush.leetcode.linkedlist;

import com.ming.commons.utils.ListNode;

/*
206 翻转链表
 */
public class Of20201219LeetCode206 {

    /*
    时间复杂度：O(n)
    空间复杂度：O(1)
     */
    public static ListNode reverseList(ListNode head) {
        ListNode newHead = null; //新链表的头指针
        ListNode next; //帮助记录久链表头指针位置
        while (head != null) {
            next = head.next;
            head.next = newHead;
            newHead = head;
            head = next;
        }
        return newHead;
    }

    public static void main(String[] args) {
        ListNode listNode = reverseList(new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4)))));
        while (listNode != null) {
            System.out.println(listNode.val);
            listNode = listNode.next;
        }
    }
}
